## --------------------------------------------------------------------------
def MedianSumma(k, n0, e0):

    ik = 0
    sn = 0.0
    r = d = et = nt = [0.]*k
    tet = [0.]*3
    
    for i in range(0, k):
        if nt[i] > 0.0:
            ik += 1
            r[i] = nt[i] / e0[i] / n0[i]
            d[i] = r[i]
            et[i] = r[i]
        if ik < 3:
            sn = 0.0
            for i in range(0,k):
                et[i] = 0.0

        jk = ik / 2 +1 # Note: Ik DIV 2 + 1
        for j in range(0,jk):
            c1 = 0.0
            for i in range(0,k):
                if r[i] >= c1:
                    c1 = r[i]
                    ii = i
            if j < jk:
                c2 = c1
                r[ii] = 0.0
        tet[1] = c1
        if jk == ik:
            tet[1] = (c1+c2)/2.0
            for i in range(0,k):
                r[i] = abs(d[i]-tet[1])
            for j in range(0,jk):
                c1 = 0.0
                for i in range(0,k):
                    if r[i] >= c1:
                        c1 = r[i]
                        ii = i
                if j < jk:
                    c2 = c1
                    r[ii] = 0.0
            tet[2] = c1
            if jk == ik:
                tet[2] = (c1+c2)/2.0
                for i in range(0, k):
                    r[i] = d[i]
                for i in range(0,k):
                    et[i] = et[i] / tet[1]
                    sig = tet[2]
                    if sig < 0.015:
                        sig = 0.015
                        sig = 4*sig
                        m = 0
                        sn = 0
                for i in range(0,k):
                    if (abs(r[i]-tet[1]) < sig):
                        sn += nt[i]
                        m +=1
                    if (m < k/4): # k div 4
                        sn = 0
                    else:
                        sn = k*sn/m
                        
        
    return(nt, et, sn)
